<?php

namespace app\api\model;
use think\Model;

class Access extends Model {
//    protected $name = 'admin_user';

    public static function accessList($field='',$where=''){
//        return static::field('id,name,parent_id')->filter(function($user) {
//            $user['value'] = $user['id'];
//            $user['label'] = $user['name'];
//            return $user;
//        })->select()->hidden(['name','id']);
        return static::where($where)->field($field)->select();
    }

    public static function accessRoleList($where = '',$w = ''){
        $Role = Role::with(['access' => function($query) use($w){
            $query ->getQuery()->where($w);
        }])->where($where)->select();
        return $Role;
    }

    public static function add($data){
        $data['create_time'] = strtotime('now');
        return $user = static::create($data);
    }

    public static function edit($data){
        $data['update_time'] = strtotime('now');
        return static::update($data);
    }

    /**
     * @param string $field
     * @param array $where
     * @return Access[]|array|\think\Collection
     * 获取菜单
     */
    public static function accessMenu($field = '',$where=[]){

        $w = [['is_menu','=',1], ['is_route','=',0]];
        $where = array_merge($where,$w);
       return static::where($where)->field($field)->select();
    }

    public static function getDataById($id=''){
       return static::where('id',$id)->find();
    }
}
